package com.gr.staffpm.pages.tasks;

import java.util.Calendar;
import java.util.Date;

import org.apache.wicket.PageParameters;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.datatypes.TaskImportance;
import com.gr.staffpm.datatypes.TaskStatus;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.model.StringModel;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.tasks.model.TaskImportanceChoiceRenderer;
import com.gr.staffpm.tasks.model.TaskStatusChoiceRenderer;
import com.gr.staffpm.tasks.model.UserChoiceRenderer;
import com.gr.staffpm.tasks.service.TaskService;

public class EditTaskForm extends Form<Task> {

    private static final long serialVersionUID = 1L;
    private final TaskService taskService;
    private final UserService userService;
    private final Task task;
    private final TextField<String> summary;
    private final DateTextField dueDate;
    private final DropDownChoice<TaskImportance> importance;
    private final DropDownChoice<TaskStatus> status;
    private final DropDownChoice<User> assignee;
    private final TextArea<String> description;

    public EditTaskForm(String id, UserService userService, TaskService taskService, final Task task) {
        super(id, new CompoundPropertyModel<Task>(task));
        this.task = task;
        this.taskService = taskService;
        this.userService = userService;

        summary = new TextField<String>("summary");
        summary.setLabel(new StringModel("Summary"));
        summary.setEscapeModelStrings(true);
        add(summary);

        dueDate = new DateTextField("dueDate", new PropertyModel<Date>(task, "dueDate"), new StyleDateConverter("F-", true));
        dueDate.setLabel(new StringModel("Summary"));
        dueDate.add(new DatePicker());
        dueDate.setEscapeModelStrings(true);
        add(dueDate);

        add(importance = new DropDownChoice<TaskImportance>("importance", taskService.getAllImportances(), new TaskImportanceChoiceRenderer()));
        importance.setLabel(new StringModel("Summary"));

        add(status = new DropDownChoice<TaskStatus>("status", taskService.getAllStatuses(), new TaskStatusChoiceRenderer()));
        status.setLabel(new StringModel("Summary"));

        add(assignee = new DropDownChoice<User>("assignee", userService.getAllUsers(), new UserChoiceRenderer()));
        assignee.setLabel(new StringModel("Summary"));

        add(description = new TextArea<String>("description"));
        description.setLabel(new StringModel("Summary"));
        description.setEscapeModelStrings(true);

        Button cancel = new Button("cancelButton") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                setResponsePage(ViewTask.class, new PageParameters("id=" + String.valueOf(task.getTaskId())));
            }

        };
        cancel.setDefaultFormProcessing(false);
        add(cancel);

    }

    /**
     * Called upon form submit. Attempts to authenticate the user.
     */
    @Override
    protected void onSubmit() {

        task.setSummary(summary.getValue());
        task.setAssignee(assignee.getModelObject());
        task.setDueDate(dueDate.getModelObject());
        task.setDescription(description.getValue());
        task.setImportance(importance.getModelObject());
        task.setStatus(status.getModelObject());
        task.setLastUpdated(Calendar.getInstance().getTime());
        task.setUpdatedBy(userService.getCurrentUser());

        taskService.updateTask(task);

        //Send user back to view page
        setResponsePage(ViewTask.class, new PageParameters("id=" + String.valueOf(task.getTaskId())));
    }

}
